﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;

namespace WeChatDataSrc
{
    class RefineFore
    {
        static string URL = ConfigurationSettings.AppSettings["RefineForeURL"];
        /// <summary>
        /// 上传数据
        /// </summary>
        public static String UploadData()
        {
            DateTime NowMaxDate = DateTime.Parse(ReadMax.ReadMaxCityFore());
            DateTime BeginDate = NowMaxDate.AddDays(1);
            List<String> result = new List<string>();
            string data = GetData(BeginDate);
            if (data == null || data == "")
            {
                return null;
            }
            string i = HttpPost.SendData(URL, data);
            return i;
        }

        private static string GetData(DateTime Time)
        {
            List<string> Result = new List<string>();
            DateTime Now = DateTime.Now;
            List<CityForeStationModel> Results = new List<CityForeStationModel>();
            while (true)
            {
                if (DateTime.Compare(Time, Now) == 0)
                {
                    break;
                }

                List<String> i = new List<String>();
                bool has_result = false;
                string[] Spans = new string[] { "FD00_24", "FD24_48", "FD48_72", "FD72_96", "FD96_120", "FD120_144", "FD144_168" };
                foreach (string span in Spans)
                {
                    string sql = string.Format("select * from Normal_CityFore where [date]='{0}' and span='FD00_24' and semitype='Auto' order by sendpoint desc", Time.ToString("yyyyMMdd"));
                    DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CityFore_ConnectionString, CommandType.Text, sql).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        has_result = true;

                        List<String> FilteredTypes = new List<string>();
                        foreach (DataRow item in dt.Rows)
                        {
                            String Type = item["type"].ToString();
                            if (FilteredTypes.Contains(Type))
                            {
                                continue;
                            }
                            if (FilteredTypes.Find((x) => x == item["type"].ToString()) != null) //已经添加过该种类型
                            {
                                //不做处理
                            }
                            else //没有添加过该种类型
                            {
                                String Value = item["value"].ToString();
                                FilteredTypes.Add(Type);
                                string[] Values = Value.Split('|');
                                foreach (string temp in Values)
                                {
                                    if (temp == "")
                                    {
                                        continue;
                                    }
                                    string[] KeyValuePair = temp.Split(';');
                                    CityForeStationModel CFSM = null;
                                    if (Results.Find(x => x.StationCode == KeyValuePair[0]) == null)
                                    {
                                        CFSM = new CityForeStationModel();
                                        Results.Add(CFSM);
                                    }
                                    else
                                    {
                                        CFSM = Results.Find(x => x.StationCode == KeyValuePair[0]);
                                    }
                                    CFSM.StationCode = KeyValuePair[0];
                                    CFSM.Data.Span = span;
                                    if (Type == "maxt")
                                    {
                                        CFSM.Data.Maxt = KeyValuePair[1];
                                    }
                                    else if (Type == "mint")
                                    {
                                        CFSM.Data.Mint = KeyValuePair[1];
                                    }
                                    else if (Type == "wd")
                                    {
                                        CFSM.Data.WindDirection = KeyValuePair[1];
                                    }
                                    else if (Type == "ws")
                                    {
                                        CFSM.Data.WindSpeed = KeyValuePair[1];
                                    }
                                    else if (Type == "wth")
                                    {
                                        CFSM.Data.Rain = KeyValuePair[1];
                                    }
                                }
                            }
                        }

                        foreach (var obj in Results)
                        {
                            string s = string.Format("date={0}&station={1}&maxt={2}&mint={3}&nowt={4}&wind_direction={5}&wind_speed={6}&rain={7}&span={8}", DateTime.Now.ToString("yyyy-MM-dd"), obj.StationCode, obj.Data.Maxt, obj.Data.Mint, obj.Data.Nowt, obj.Data.WindDirection, obj.Data.WindSpeed, obj.Data.Rain, span);
                            i.Add(s);
                        }
                    }
                }
                if (has_result == true)
                {
                    Result.AddRange(i);
                }
                else
                {

                }

                Time = Time.AddDays(1);
            }
            if (Results.Count == 0)
            {
                return null;
            }
            StringBuilder sb = new StringBuilder();
            System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            ser.Serialize(Results, sb);
            return sb.ToString();
        }

        private class CityForeStationModel
        {
            public CityForeStationModel()
            {
                this.StationCode = "";
                this.Data = new CityForeDataModel();
            }
            public string StationCode { get; set; }
            public CityForeDataModel Data { get; set; }
        }

        private class CityForeDataModel
        {
            public CityForeDataModel()
            {
                this.Maxt = "";
                this.Mint = "";
                this.Nowt = "";
                this.WindDirection = "";
                this.WindSpeed = "";
                this.Rain = "";
                this.Span = "";
            }
            public string Maxt { get; set; }
            public string Mint { get; set; }
            public string WindDirection { get; set; }
            public string WindSpeed { get; set; }
            public string Rain { get; set; }
            public string Nowt { get; set; }
            public string Span { get; set; }
        }
    }
}
